var express = require('express');
var router = express.Router();
const md5 = require('md5-node')
// 自定义模块
const query = require('../../utils/mysql');
const Result = require('../../utils/api');
const {
    createToken
} = require('../../utils/jwt');

/**
 * 授权登录接口
 */
router.post('/view/login', async function (req, res, next) {
    let account = req.body.username;
    let password = req.body.password;

    let sql = 'SELECT * FROM blog_admin WHERE account = ?'
    let arrSql = [account]

    let arr = await query(sql, arrSql)
    if (arr.length == 0) {
        Result.fail(res, "暂无此用户");
    } else if (arr[0].password == md5(password)) {
        let token = createToken(account)
        Result.success(res, {
            token: token
        })
    } else {
        Result.fail(res, "密码错误")
    }

});

/**
 * 配置信息详情
 */

router.post('/view/userInfo', async function (req, res, next) {
    try {
        let sql = 'SELECT * FROM blog_config'
        let [data] = await query(sql)
        if (!data) {
            let data = {
                id: 0,
                logo: "",
                avatar: "",
                name: "",
                quotes: "",
                qq: "",
                netease_cloud: "",
                bilibili: "",
                gitee: "",
                bgimg: "",
            }
            Result.success(res, data)
        } else {
            Result.success(res, data)
        }
    } catch (error) {
        Result.fail(res, error)
    }
})

module.exports = router;